package cn.thread;

import java.io.*;
import java.net.URL;

public class RDownload implements Runnable{
    private String Url;
    private String localhost;
    public RDownload(String Url,String localhost){
        this.Url=Url;
        this.localhost=localhost;
    }
    public static void main(String args[]){
        RDownload rd01=new RDownload("http://www.baidu.com","gao.txt");
        RDownload rd02=new RDownload("http://www.baidu.com","shan.txt");
        RDownload rd03=new RDownload("http://www.baidu.com","qing.txt");
        new Thread(rd01).start();
        new Thread(rd02).start();
        new Thread(rd03).start();
    }

    @Override
    public void run() {
        URLDownload ud=new URLDownload();
        ud.urlDownload(Url,localhost);

    }
}
class URLDownload {
    //现在图片地址，文件存放路径
    public void urlDownload(String Url, String localhost) {
        File url = new File(Url);
        File file = new File(localhost);
        try (InputStreamReader isr = new InputStreamReader(new BufferedInputStream(new URL(Url).openStream()));
              OutputStreamWriter osw=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(localhost)))
        ) {
           char[] chars=new char[1024];
            int temp=0;
            while ((temp=(isr.read(chars)))!=-1){
                osw.write(chars,0,temp);
            }

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}